﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace LANmine
{
    public partial class CateringForm : Form
    {
        private Language lmLang;
        private User lmUser;
        private String bestellingsnummer;

        Form frmDetailsBestelling;

        public CateringForm(Language lmLang, User lmUser)
        {
            this.lmLang = lmLang;
            this.lmUser = lmUser;

            InitializeComponent();
            ApplyLanguage();
            loadProducten();
            loadOrders();
        }

        #region Language Specifications
        /**
         * MainForm::ApplyLanguage()
         * ---------------------------
         * Overwrite all text properties of all elements in all forms with the right ones.
         */
        private void ApplyLanguage()
        {
            /**
             * Header label
             */
            this.lblCatering.Text = this.lmLang.GetString("menu.btnCatering");
            /**
             * other labels & buttons
             */
            this.lblLstProducts.Text = this.lmLang.GetString("catering.lblLstProducts");
            this.lblLstOrders.Text = this.lmLang.GetString("catering.lblLstOrders");
            this.lblOrdersInProgress.Text = this.lmLang.GetString("catering.lblOrdersInProgress");
            this.btnOrder.Text = this.lmLang.GetString("catering.btnOrder");
        }
        #endregion

        /**
         * Event Triggers
         */
        private void CateringForm_Resize(object sender, EventArgs e)
        {
            btnAddOne.Width = tlpNavProducts.Width;
            btnRemOne.Width = tlpNavProducts.Width;
            btnRemAll.Width = tlpNavProducts.Width;
        }

        private void CateringForm_ResizeEnd(object sender, EventArgs e)
        {
            btnAddOne.Width = tlpNavProducts.Width;
            btnRemOne.Width = tlpNavProducts.Width;
            btnRemAll.Width = tlpNavProducts.Width;
        }

        /**Verwijdert alle producten van je bestelling*/
        private void btnRemAll_Click(object sender, EventArgs e)
        {
            lbxBestellingen.Items.Clear();
        }
        
        // Zorgt ervoor dat er meteen een item wordt aangeduidt.
        private void CateringForm_Load(object sender, EventArgs e)
        {
            lbxProducten.SelectedItem = lbxProducten.Items[0];
        }

        private void btnAddOne_Click(object sender, EventArgs e)
        {
            lbxBestellingen.Items.Add(lbxProducten.SelectedItem.ToString());
        }

        private void btnRemOne_Click(object sender, EventArgs e)
        {
            if(lbxBestellingen.SelectedIndex != -1)
                lbxBestellingen.Items.RemoveAt(lbxBestellingen.SelectedIndex);
        }

        private void btnOrder_Click(object sender, EventArgs e)
        {
            Database dbBestelling = new Database("rw");
            SqlConnection dbConn = dbBestelling.getDbConn();

            dbConn.Open();
            SqlDataAdapter daBestelling = new SqlDataAdapter("SELECT product_id, deelnemer_id, bestellingsnummer FROM catering_bestellingen", dbConn);
            dbConn.Close();

            DataSet dsBestelling = new DataSet();
            DataTable dtBestelling = new DataTable("Bestelling"); ;
            daBestelling.Fill(dtBestelling);
            
            dsBestelling.Tables.Add(dtBestelling);
            DataRow drCurrent;

            /** het vullen van het product_id*/
            dbConn.Open();
            Int16 i;
            String[] product_id = new String[lbxBestellingen.Items.Count + 1];
            for (i = 0; i < lbxBestellingen.Items.Count; i++)
            {
                SqlDataAdapter daProduct = new SqlDataAdapter("SELECT id FROM catering_producten WHERE naam LIKE '" + lbxBestellingen.Items[i].ToString().Substring(0, 5) + "%'", dbConn);

                DataTable dtProduct = new DataTable("Product");
                daProduct.Fill(dtProduct);

                product_id[i] = dtProduct.Rows[0]["id"].ToString();
            }
            dbConn.Close();

            /** het vullen van het bestellingsnummer*/
            dbConn.Open();
            SqlDataAdapter daBestellingsnummer = new SqlDataAdapter("SELECT TOP 1 bestellingsnummer FROM catering_bestellingen ORDER BY bestellingsnummer desc", dbConn);
            dbConn.Close();

            DataTable dtBestellingsnummer = new DataTable("Bestelling");
            daBestellingsnummer.Fill(dtBestellingsnummer);
            String nummer = dtBestellingsnummer.Rows[0]["Bestellingsnummer"].ToString();
            dbConn.Open();
            try
            {
                    i = 0;
                    foreach (object o in lbxBestellingen.Items)
                    {
                        drCurrent = dtBestelling.NewRow();
                        drCurrent["product_id"] = Convert.ToInt32(product_id[i]);
                        drCurrent["deelnemer_id"] = Convert.ToInt32(lmUser.ID);
                        drCurrent["bestellingsnummer"] = (Convert.ToInt32(nummer) + 1);
                        dtBestelling.Rows.Add(drCurrent);
                        drCurrent = null;
                        i++;

                    }
                    string sqlInsert = "INSERT INTO catering_bestellingen (product_id, deelnemer_id, bestellingsnummer) VALUES (@product_id, @deelnemer_id, @bestellingsnummer)";

                    SqlCommand cmdInsert = new SqlCommand(sqlInsert, dbConn);
                    cmdInsert.Parameters.Add("@product_id", SqlDbType.Int, 4, "product_id");
                    cmdInsert.Parameters.Add("@deelnemer_id", SqlDbType.Int, 4, "deelnemer_id");
                    cmdInsert.Parameters.Add("@bestellingsnummer", SqlDbType.Int, 4, "bestellingsnummer");

                    daBestelling.InsertCommand = cmdInsert;
                    daBestelling.Update(dsBestelling, "Bestelling");

                    lbxBestellingen.Items.Clear();
                    
                    SqlDataAdapter daOrders = new SqlDataAdapter("SELECT p.naam, b.bestellingsnummer, p.prijs from catering_bestellingen b inner join catering_producten p on p.id = b.product_id where b.bestellingsnummer = " + (Int32.Parse(nummer) + 1), dbConn);
                    dbConn.Close();
                    DataSet dsOrders = new DataSet("Orders");

                    daOrders.Fill(dsOrders, "Orders");

                    DataTable dtOrders;
                    dtOrders = dsOrders.Tables["Orders"];

                    foreach (DataRow dr in dtOrders.Rows)
                    {
                        lbxOIProgress.Items.Add(dr["bestellingsnummer"].ToString() + " " + dr["naam"].ToString() + " " + dr["prijs"].ToString());

                    }

                    
            }
            catch (Exception ex)
            {
                throw new Exception(ex.ToString(), ex);
            }
            finally
            {
                dbConn.Close();
            }
        }

        private void loadProducten()
        {
            Database dbProducten = new Database("rw");
            SqlConnection dbConn = dbProducten.getDbConn();
            dbConn.Open();

            SqlDataAdapter daProducten = new SqlDataAdapter("Select * From catering_producten", dbConn);
            dbConn.Close();
            DataSet dsProducten = new DataSet("Product");

            daProducten.FillSchema(dsProducten, SchemaType.Source, "Product");
            daProducten.Fill(dsProducten, "Product");

            DataTable dtProducten;
            dtProducten = dsProducten.Tables["Product"];

            foreach (DataRow drCurrent in dtProducten.Rows)
            {
                lbxProducten.Items.Add(drCurrent["naam"].ToString() + " " + drCurrent["prijs"].ToString());

            }
        }

        private void loadOrders()
        {
            Database dbOrders = new Database("rw");
            SqlConnection dbConn = dbOrders.getDbConn();
            dbConn.Open();


            SqlDataAdapter daOrders = new SqlDataAdapter("SELECT b.bestellingsnummer, SUM(p.prijs) as prijs FROM catering_bestellingen b INNER JOIN catering_producten p ON p.id = b.product_id WHERE b.deelnemer_id = " + lmUser.ID + " GROUP BY b.bestellingsnummer" , dbConn);
            dbConn.Close();
            DataSet dsOrders = new DataSet("Orders");

            daOrders.Fill(dsOrders, "Orders");

            DataTable dtOrders;
            dtOrders = dsOrders.Tables["Orders"];

            foreach (DataRow r in dtOrders.Rows)
            {
                lbxOIProgress.Items.Add(new ItemData(r["bestellingsnummer"].ToString(), ("Bestellingsnummer: " + r["bestellingsnummer"].ToString() + " Prijs:" + r["prijs"].ToString())));
            }

            
        }

        /**aanpassen van de taal nog*/
        private void lbxOIProgress_DoubleClick(object sender, EventArgs e)
        {
            this.frmDetailsBestelling = new DetailsBestellingForm(lmLang, bestellingsnummer);
            this.frmDetailsBestelling.Text = "Details van de bestellingen";
            this.frmDetailsBestelling.ShowDialog();
        }

        private void CateringForm_MouseCaptureChanged(object sender, EventArgs e)
        {
            bestellingsnummer = ((ItemData)lbxOIProgress.SelectedItem).bestellingsnummer.ToString();
        }

    }

}

